<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<style>
    *,
    *:before,
    *:after {
        box-sizing: border-box;
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
    }
    html{
        line-height: 1.15;
        -ms-text-size-adjust: 100%;
        -webkit-text-size-adjust: 100%;
    }
    html,
    body {
        margin: 0;
        height: 100%;
    }
    body {
        font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", SimSun, sans-serif;
        color: rgba(0, 0, 0, .65);
        font-size: 14px;
        line-height: 1.8;
        background: #F7F7F7;
        transition: background 1s cubic-bezier(0.075, 0.82, 0.165, 1);
        overflow-y: scroll;
        -webkit-user-select: initial !important;
        user-select: initial !important;
    }
    .main-container {
        width: 1000px;
        margin: 0 auto;
        position: relative;
        padding: 0 44px;
        border-left: 1px solid #EEE;
        background: #FFF;
    }

    /*
     *
     *** doc content ***
     *
     */
    .markdown {
        padding: 30px 0;
        min-height: 480px;
        overflow: hidden;
        font-size: 14px;
        line-height: 1.8;
        color: rgba(0, 0, 0, .65);
    }
    .markdown table {
        border-collapse: collapse;
        border-spacing: 0;
        empty-cells: show;
        vertical-align: top;
        width: 100%;
        overflow-x: auto;
        overflow-y: hidden;
        -webkit-overflow-scrolling: touch;
        margin: 12px 0 24px;
    }
    .markdown table + p {
        margin-top: 24px;
    }
    .markdown table th,
    .markdown table td {
        padding: 8px 12px;
        font-size: 12px;
        text-align: left;
        word-break: break-word;
        word-wrap: break-word;
        min-width: 100px;
    }
    .markdown table tbody td {
        border-bottom: 1px solid #F0F3F6;
    }
    /** 表格标题，兼容云雀编辑器产生的 thead>td **/
    .markdown table th, .markdown thead td {
        white-space: nowrap;
        font-weight: 600;
        background-color: #F7F7F7;
        border: 1px solid transparent;
    }
    .markdown table th:first-child {
        border-top-left-radius: 4px;
    }
    .markdown table th:last-child {
        border-top-right-radius: 4px;
    }
    .markdown table img {
        max-width: 240px;
        height: auto;
    }
    .markdown a {
        cursor: pointer;
    }

    .markdown h1,
    .markdown h2,
    .markdown h3,
    .markdown h4,
    .markdown h5,
    .markdown h6,
    .markdown p,
    .markdown pre {
        margin: 1em 0;
    }
    .markdown h1,
    .markdown h2,
    .markdown h3,
    .markdown h4,
    .markdown h5,
    .markdown h6 {
        word-wrap: break-word;
    }
    .markdown h1,
    .markdown h2,
    .markdown h3 {
        clear: both;
        color: rgba(0, 0, 0, .85);
    }
    .markdown > h1 {
        margin-top: 40px;
        font-size: 30px;
        line-height: 38px;
    }
    #title,
    .markdown > h1:first-child {
        margin-top: 0;
    }
    .markdown h2 {
        color: #e4393c;
        margin-top: 40px;
        font-size: 24px;
        line-height: 32px;
    }
    .markdown h3 {
        margin-top: 32px;
        font-size: 16px;
        line-height: 24px;
    }
    .main-container .markdown code {
        margin: 0 3px;
        background: #F7F7F7;
        padding: 1px 6px;
        border-radius: 2px;
        border: 1px solid #e9e9e9;
        color: #555;
        font-size: 12px;
        font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
    }
    .main-container .markdown pre,
    .main-container .markdown pre code {
        background: #F7F7F7;
        border-radius: 4px;
        border: 1px solid #EEE;
        font-size: 12px;
        color: #666;
        font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
    }
    .main-container .markdown pre {
        padding: 0px 24px;
        overflow: auto;
        display: block;
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        padding-top: 1em;
    }
    .main-container .markdown pre p {
        margin: 0;
    }
    .main-container .markdown pre code {
        margin: 0;
        padding: 0;
        border: none;
    }
</style>
<body>
<div class="main-container"></div>
</body>
<script src="/plugins/common.js"></script>
<script src="/plugins/jquery-1.8.3.min.js"></script>
<script>
    var docUrl = SERVER_URL+"/moduleInfo/moduleDocument?moduleCode="+urlParam("moduleCode");

    //初始化文档信息
    function initDocInfo(data) {
        if(data){
            var str = "";
            for(var i in data.paths){
                var docInfo = data.paths[i];
                var requestType = docInfo.get ? "GET" : "POST";
                var summary = docInfo.get ? docInfo.get.summary : docInfo.post.summary;
                var host = data.host;
                var docParam = docInfo.get ? docInfo.get.parameters : docInfo.post.parameters;
                var description = docInfo.get ? docInfo.get.description : docInfo.post.description;
                str += ["<div class=\"markdown\">",
                    "        <h2 id=\"docTitle\">接口名称:&nbsp;"+summary+"</h2>",
                    "        <div>",
                    "            <h3>请求地址</h3>",
                    "            <table>",
                    "                <tbody>",
                    "                <tr>",
                    "                    <th>环境</th>",
                    "                    <th>HTTPS请求地址</th>",
                    "                    <th>请求方式</th>",
                    "                </tr>",
                    "                <tr>",
                    "                    <td>正式环境</td>",
                    "                    <td id=\"docUrl\">"+host+i+"</td>",
                    "                    <td>"+requestType+"</td>",
                    "                </tr>",
                    "                </tbody>",
                    "            </table>",
                    "            <h3>请求参数</h3>",
                    "            <table>",
                    "                <tbody>",
                    "                <tr>",
                    "                    <th class=\"prop-name\">参数</th>",
                    "                    <th class=\"prop-type\">类型</th>",
                    "                    <th class=\"prop-required\">是否必填</th>",
                    "                    <th class=\"prop-desc\">描述</th>",
                    "                </tr>"].join("");
                    for(var pi in docParam){
                        str +=  ["    <tr>",
                            "            <td>"+docParam[pi].name+"</td>",
                            "            <td>"+docParam[pi].type+"</td>",
                            "            <td>必填</td>",
                            "            <td>"+docParam[pi].description+"</td>",
                            "        </tr>"].join("");
                    }
                    str += ["        </tbody>",
                    "            </table>"].join("");
                    str += ["    <h3>响应示例</h3>",
                            "    <div class=\"code\">",
                            "		  <pre>"+formateJson(description)+"</pre>",
                            "    </div>"].join("");
                    str += [" </div>",
                    "</div>"].join("");
            }
        }

        $(".main-container").append(str);
    }

    $(function () {
        if(urlParam("moduleCode")){
            $.ajax({
                type: 'GET',
                url: docUrl,
                success: function (result) {
                    initDocInfo(result);
                }
            });
        }
    })

    //地址栏参数
    function urlParam(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null)	return decodeURIComponent(r[2]);
        return null;
    }

    //格式化json
    function formateJson(json, options) {
        var reg = null,
            formatted = '',
            pad = 0,
            PADDING = '    ';
        options = options || {};
        options.newlineAfterColonIfBeforeBraceOrBracket = (options.newlineAfterColonIfBeforeBraceOrBracket === true) ? true : false;
        options.spaceAfterColon = (options.spaceAfterColon === false) ? false : true;
        if (typeof json !== 'string') {
            json = JSON.stringify(json);
        } else {
            json = JSON.parse(json);
            json = JSON.stringify(json);
        }
        reg = /([\{\}])/g;
        json = json.replace(reg, '\r\n$1\r\n');
        reg = /([\[\]])/g;
        json = json.replace(reg, '\r\n$1\r\n');
        reg = /(\,)/g;
        json = json.replace(reg, '$1\r\n');
        reg = /(\r\n\r\n)/g;
        json = json.replace(reg, '\r\n');
        reg = /\r\n\,/g;
        json = json.replace(reg, ',');
        if (!options.newlineAfterColonIfBeforeBraceOrBracket) {
            reg = /\:\r\n\{/g;
            json = json.replace(reg, ':{');
            reg = /\:\r\n\[/g;
            json = json.replace(reg, ':[');
        }
        if (options.spaceAfterColon) {
            reg = /\:/g;
            json = json.replace(reg, ':');
        }
        (json.split('\r\n')).forEach(function (node, index) {
                var i = 0,
                    indent = 0,
                    padding = '';

                if (node.match(/\{$/) || node.match(/\[$/)) {
                    indent = 1;
                } else if (node.match(/\}/) || node.match(/\]/)) {
                    if (pad !== 0) {
                        pad -= 1;
                    }
                } else {
                    indent = 0;
                }

                for (i = 0; i < pad; i++) {
                    padding += PADDING;
                }

                formatted += padding + node + '\r\n';
                pad += indent;
            }
        );
        return formatted;
    };
</script>
</html>